import {AppstoreOutlined, ControlOutlined, HomeOutlined, PhoneOutlined} from '@ant-design/icons-vue'
import {getMenuList} from '@/apis/system/base'
import {getLocalMenus, setLocalMenus} from '@/utils/local'
import {h} from 'vue';

type menuType = {
    key: string
    icon?: () => any
    label: string
    path?: string
    children?: menuType[]
}
/*
  menuId
  menuType = 'M' / 'C' 是菜单, 'F'是功能权限
  name
  parentId
  path
  icon
*/
type IconName = keyof typeof iconMap
const iconMap = {
    HomeOutlined,
    AppstoreOutlined,
    ControlOutlined,
    PhoneOutlined
}
const transformTree = (tree: any) => {
    return tree.map((node: any) => {
        const isPermission = node.children && node.children.find((item: any) => item.menuType === 'F')
        const iconName: IconName = (node.iconName || 'HomeOutlined') as IconName
        return {
            label: node.name,
            key: node.menuId,
            path: node.path,
            icon: node.menuType === 'M' ? () => h(iconMap[iconName]) : undefined,
            iconName: node.iconName,
            menuType: node.menuType,
            parentId: node.parentId,
            children: node.children && !isPermission ? transformTree(node.children) : undefined // 递归处理子节点
        }
    })
}
export const getMenus = () => {
    const menuArr = getLocalMenus()
    if (menuArr) {
        const arr = JSON.parse(menuArr)
        arr.forEach((item: any) => {
            const iconName: IconName = (item.iconName || 'HomeOutlined') as IconName
            item.icon = () => h(iconMap[iconName])
        })
        return arr
    }
    return new Promise(async (resolve) => {
        const res = await getMenuList({})
        const menuArr = [
            {
                key: 0,
                icon: () => h(HomeOutlined),
                label: '首页',
                path: '/',
                iconName: 'HomeOutlined'
            },
            ...transformTree(res.data)
        ]
        setLocalMenus(JSON.stringify(menuArr))
        return resolve(menuArr)
    })
}
export const menus: Array<menuType> = [
    {
        key: 'sub1',
        icon: () => h(AppstoreOutlined),
        label: '系统管理',
        children: [
            {
                key: '2',
                label: '用户管理',
                path: '/userManagement'
            },
            {
                key: '3',
                label: '角色管理',
                path: '/roleManagement'
            },
            {
                key: '4',
                label: '菜单管理',
                path: '/menuManagement'
            },
            {
                key: '5',
                label: '日志管理',
                path: '/logManagement'
            }
        ]
    },
    {
        key: 'sub2',
        icon: () => h(ControlOutlined),
        label: 'FS配置管理',
        children: [
            {
                key: '6',
                label: '配置管理',
                path: '/fsConfigureManagement'
            },
            {
                key: '7',
                label: '模块配置管理',
                path: '/fsModuleConfigureManagement'
            },
            {
                key: '8',
                label: '网关管理',
                path: '/gatewayManagement'
            },
            {
                key: '9',
                label: '访问控制管理',
                path: '/accessControl'
            },
            {
                key: '10',
                label: '拨号计划管理',
                path: '/dialPlanManagement'
            }
        ]
    },
    {
        key: 'sub3',
        icon: () => h(PhoneOutlined),
        label: '呼叫管理',
        children: [
            {
                key: '11',
                label: '坐席管理',
                path: '/agentManagement'
            },
            {
                key: '12',
                label: '号码路由管理',
                path: '/phoneNumberRoute'
            },
            {
                key: '13',
                label: 'SIP号码管理',
                path: '/sipPhoneNumber'
            },
            {
                key: '14',
                label: '号码管理',
                path: '/phoneNumber'
            },
            {
                key: '15',
                label: '号码池管理',
                path: '/phonePool'
            },
            {
                key: '16',
                label: '语音文件管理',
                path: '/voiceFile'
            },
            {
                key: '17',
                label: '日程管理',
                path: '/scheduleManagement'
            }
        ]
    }
]
